package com.example.adms.leave.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.adms.leave.dto.*;
import com.example.adms.leave.service.LeaveService;
import com.example.adms.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hyt
 * @since 2023-04-20
 */
@RestController
@RequestMapping("/leave")
public class LeaveController {
    @Autowired
    LeaveService leaveService;
    /**
     * 添加请假
     * @param msg
     * @return
     */
    @PostMapping("/addLeave")
    public Result addLeave(@RequestBody leaveMsg msg) {
        int flag = leaveService.addLeave(msg);
        if (flag == 1){
            return Result.success(null);
        }else if (flag == 2){
            return Result.fail(501,"请假次数已超过系统规定次数次");
        }else {
            return Result.fail(502,"插入失败");
        }
    }

    /**
     * 查询某人请假次数
     * @param userId
     * @return
     */
    @GetMapping("/getLeaveTime")
    public Result getLeaveTime(String userId){
        return Result.success(leaveService.leaveTime(userId));
    }


    @PostMapping("/getLeaveList")
    public Result getLeaveList(@RequestBody QueryLeave queryLeave){
        Map<String,Object> map = leaveService.getLeaveList(queryLeave);
        return Result.success(map);
    }

    @PostMapping("/leaderGetLeaveList")
    public Result leaderGetLeaveList(@RequestBody QueryLeave queryLeave){
        Map<String,Object> map = leaveService.LeaderGetLeaveList(queryLeave);
        return Result.success(map);
    }

    @PostMapping("/employeeGetLeaveList")
    public Result employeeGetLeaveList(@RequestBody QueryLeave queryLeave){
        Map<String,Object> map = leaveService.employeeLeaveList(queryLeave);
        return Result.success(map);
    }

    /**
     * 获取某条请假数据
     * @param leaveId
     * @return
     */
    @GetMapping("/getLeaveDetails")
    public Result getLeaveDetails(int leaveId){
        LeaveDetails leaveDetails = leaveService.getLeaveDetails(leaveId);
        if (!ObjectUtils.isEmpty(leaveDetails)){
            return Result.success(leaveDetails);
        }
        else {
            return Result.fail(501,"查询失败");
        }
    }

    /**
     * 审批请假
     * @param examineLeave
     * @return
     */
    @PostMapping("/examineLeave")
    public Result examineLeave(@RequestBody ExamineLeave examineLeave){
        boolean result = leaveService.examineLeave(examineLeave);
        if (result){
            return Result.success(null);
        }
        else {
            return Result.fail(501,"审批失败");
        }
    }

    /**
     * 删除请假
     * @param leaveId
     * @return
     */
    @GetMapping("/deleteLeave")
    public Result deleteLeave(int leaveId){
        boolean result = leaveService.deleteLeave(leaveId);
        if (result){
            return Result.success(null);
        }
        else {
            return Result.fail(501,"删除失败");
        }
    }

    @PostMapping("/leaveReport")
    public Result leaveReport(@RequestBody leaveReport report){
        List<Object> list = leaveService.leaveReport(report);
        return Result.success(list);
    }

}
